package com.ruoyi.quartz.constant;


import com.ruoyi.common.utils.StringUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.util.ResourceBundle;


public class Resource {
	protected final Log log = LogFactory.getLog(Resource.class);
	
	private ResourceBundle resourceBundle;
	private static Resource instance;
	private long lastupdateTime;
	
	
	/**
	 * 获取JOB执行状态
	 * @return
	 */
	public static boolean isJobRun(){
		boolean jobRunnable = false;
		try {
			Resource pc = Resource.getInstance();
			if("true".equalsIgnoreCase(pc.getProperty("job.enabled"))){
				jobRunnable = true;
			}else{
				jobRunnable = false;
			}
		} catch (Exception e) {
			jobRunnable = false;
		}
		return jobRunnable;
	}
	
	
	public synchronized static Resource getInstance() {
		if (instance == null) {
			instance = new Resource();
			instance.init();
		}else if (instance.isExpired()){
			instance.init();
		}
		return instance;
	}
	
	private void init() {
		resourceBundle = ResourceBundle.getBundle("system");
		lastupdateTime = System.currentTimeMillis();
	}

	private boolean isExpired() {
		return (System.currentTimeMillis()-lastupdateTime) >3600*1000;
	}

 
	
	public String getProperty(String key){
		try{
			String value=resourceBundle.getString(key);
			if(StringUtil.isNotEmpty(value)){
				return value.trim();
			}
			return value;
		}catch(Exception e) {
			log.error(" key: " + key + " not found.");
			e.printStackTrace();
		}
		return "";
	}
}
